<template>
  <div class="wrapper">
    <img
      src="http://www.dell-lee.com/imgs/vue3/user.png"
      alt=""
      class="wrapper__userImg"
    />
    <div class="wrapper__inputBox">
      <input
        type="text"
        class="wrapper__inputBox__input"
        placeholder="请输入用户名"
        v-model="username"
      />
      <div class="wrapper__inputBox__gap"></div>
      <input
        type="password"
        class="wrapper__inputBox__input"
        placeholder="请输入密码"
        autocomplete="new-password"
        v-model="password"
      />
      <div class="wrapper__inputBox__gap"></div>
      <input
        type="password"
        class="wrapper__inputBox__input"
        placeholder="确认密码"
        autocomplete="new-password"
        v-model="confirmpassword"
      />
    </div>
    <button class="wrapper__btn" @click="handleRegister">注册</button>
    <div class="wrapper__registered">
      <a
        href="javascript:;"
        class="wrapper__registered__link"
        @click="handleLoginClick"
        >已有账号去登陆</a
      >
    </div>
    <Toast v-if="show" :message="toastMessage" />
  </div>
</template>

<script>
import { reactive, toRefs } from "vue";
import { useRouter } from "vue-router";
import { post } from "../../utils/request";
import Toast, { useToastEffect } from "../../components/Toast";

//处理注册相关逻辑
const useRegisterEffect = (showToast) => {
  const router = useRouter();
  const data = reactive({ username: "", password: "", confirmpassword: "" });
  const handleRegister = async () => {
    try {
      // const { username, password, confirmpassword } = data;
      // if(!username || !password && password != confirmpassword) {
      //   showToast("");
      //   return
      // }
      const result = await post("/api/user/register/", {
        username: data.username,
        password: data.password,
      });
      if (result?.errno === 0) {
        //利用路由的push方法跳转到指定的页面
        router.push({ name: "Login" });
      } else {
        //调用已经封装好的弹窗函数
        showToast("注册失败");
      }
    } catch (e) {
      //调用已经封装好的弹窗函数
      showToast("请求失败");
    }
  };
  const { username, password, confirmpassword } = toRefs(data);
  return { username, password, confirmpassword, handleRegister };
};

//处理登录跳转
const useLoginEffect = () => {
  const router = useRouter();
  const handleLoginClick = () => {
    router.push({ name: "Login" });
  };
  return { handleLoginClick };
};

export default {
  name: "Register",
  components: { Toast },
  setup() {
    //这是公共部分的弹窗功能，只要提示登录失败或请求失败的逻辑
    const { show, toastMessage, showToast } = useToastEffect(showToast);
    const {
      username,
      password,
      confirmpassword,
      handleRegister,
    } = useRegisterEffect();
    const { handleLoginClick } = useLoginEffect();
    return {
      username,
      password,
      show,
      toastMessage,
      confirmpassword,
      handleRegister,
      handleLoginClick,
    };
  },
};
</script>

<style lang="scss" scoped>
.wrapper {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  transform: translateY(-50%);
  padding: 0 0.4rem;
  &__userImg {
    width: 0.66rem;
    height: 0.66rem;
    display: block;
    margin: 0 auto 0.4rem;
  }
  &__inputBox {
    display: flex;
    flex-wrap: wrap;
    &__input {
      width: 100%;
      padding: 0.12rem 0 0.12rem 0.16rem;
      border: 0.01rem solid rgba(0, 0, 0, 0.1);
      border-radius: 0.06rem;
      background-color: #f9f9f9;
      font-size: 0.16rem;
      line-height: 0.24rem;
      color: rgba(0, 0, 0, 0.5);
      &::placeholder {
        color: rgba(0, 0, 0, 0.5);
      }
    }
    &__gap {
      width: 100%;
      height: 0.16rem;
    }
  }
  &__btn {
    width: 100%;
    margin: 0.16rem auto;
    padding: 0.12rem 0;
    font-size: 0.16rem;
    color: #ffffff;
    line-height: 0.24rem;
    border-radius: 0.04rem;
    background-color: #0091ff;
    box-shadow: 0 0.04rem 0.08rem 0 rgba(0, 145, 255, 0.32);
  }
  &__registered {
    display: flex;
    justify-content: center;
    align-items: center;
    &__link {
      font-size: 0.14rem;
      color: rgba(0, 0, 0, 0.5);
      line-height: 0.2rem;
    }
    &__gap {
      height: 0.13rem;
      margin: 0 0.12rem;
      border-left: 0.01rem solid rgba(0, 0, 0, 0.5);
      border-right: 0.01rem solid rgba(0, 0, 0, 0.5);
    }
  }
}
</style>